// pages/yingqi/yingqi.js
const log = require('../log/log.js')
const util = require('../utils/util.js')
const sizhu = require('../sizhu/sizhu.js')
const app = getApp();

//十个天干         0     1     2     3    4     5     6     7    8     9
const ganList = ["甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"]
//十二个地支       0     1     2     3    4     5     6     7     8     9    10   11
const zhiList = ["子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"]
//60甲子
let jiaZiList = []
//节令
const jieQiList = ['大雪', '冬至', '小寒', '大寒', '立春', '雨水', '惊蛰', '春分', '清明', '谷雨', '立夏', '小满', '芒种', '夏至', '小暑', '大暑', '立秋', '处暑', '白露', '秋分', '寒露', '霜降', '立冬', '小雪']
Page({

    /**
     * 页面的初始数据
     */
    data: {
        scrHeight: 600
    },
    onShareAppMessage: function () {
        return {
            title: '分享六爻策给你的伙伴吧',
            path: '/pages/index/index',
            imageUrl: '/images/6YCSHARE.png'
        }
    },
    autoStyle: function () {
        var systeminfo = wx.getSystemInfoSync();
        log.info('index-autoStyle-获取系统参数', systeminfo)

        //px为单位
        var sW = systeminfo.screenWidth
        var sH = systeminfo.screenHeight
        this.sH = sH

        var rate = 750 / sW;

        //rpx为单位
        var rW = parseInt(sW * rate)
        var rH = parseInt(sH * rate)

        //屏幕长宽比
        var hwNum = sH / sW

        //pad的参数
        var pam = 1
        if (hwNum < 1.4) {
            pam = 0.45
        }

        var scrHeight = parseInt(rH * 0.75)

        var yearW = parseInt(rW * 0.17)
        var monthW = parseInt(rW * 0.13)
        var dayW = parseInt(rW * 0.62)
        var mH = parseInt(rH * 0.019 * pam)

        var contentHeight = parseInt(rH * 0.0406)
        var dayH = parseInt(rH * 0.8806)
        var yearCW = parseInt(rW * 0.17)
        var monthCW = parseInt(rW * 0.13)
        var dayCW = parseInt(rW * 0.62)

        var yearNumW = parseInt(rW * 0.09)
        var yearGzW = parseInt(rW * 0.08)

        var monthNumW = parseInt(rW * 0.05)
        var mongthGzW = parseInt(rW * 0.08)

        var dayNumW = parseInt(rW * 0.05)
        var dayGzW = parseInt(rW * 0.08)
        var lunarW = parseInt(rW * 0.16)
        var weekW = parseInt(rW * 0.08)
        var jieQiNameW = parseInt(rW * 0.08)
        var jieQiDateW = parseInt(rW * 0.17)

        this.setData({
            scrHeight: scrHeight,
            yearW: yearW,
            monthW: monthW,
            dayW: dayW,
            mH : mH,
            contentHeight: contentHeight,
            dayH: dayH,
            yearCW: yearCW,
            monthCW: monthCW,
            dayCW: dayCW,
            yearNumW: yearNumW,
            yearGzW: yearGzW,
            monthNumW: monthNumW,
            mongthGzW: mongthGzW,
            dayNumW: dayNumW,
            dayGzW: dayGzW,
            lunarW: lunarW,
            weekW: weekW,
            jieQiNameW: jieQiNameW,
            jieQiDateW: jieQiDateW
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.autoStyle()

        //初始化
        this.getJiaZi()

        var date = app.globalData.nowDate
        var year = date.getFullYear()
        var month = date.getMonth() + 1
        var day = date.getDate()
        this.queryYear = year
        this.showGanZhi(year, month, day)
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },
    getInputValue: function (e) {
        this.setData({
            queryInput: e.detail.value
        })
    },
    query: function () {
        var inputVal = this.data.queryInput

        if (!inputVal) {
            var date = new Date()
            var year = date.getFullYear()
            var month = date.getMonth() + 1
            var day = date.getDate()
            this.queryYear = year
            this.showGanZhi(year, month, day)
        }

        var reg = /^[0-9]+$/;
        if (inputVal != "" && inputVal != undefined && inputVal != null && !reg.test(inputVal)) {
            wx.showToast({
                title: '请输入年份',
                icon: 'none',
                duration: 1500
            })
            return;
        } else if (inputVal != "" && reg.test(inputVal) && (inputVal < 1901 || inputVal > 2089)) {
            wx.showToast({
                title: '年份范围1901 - 2089',
                icon: 'none',
                duration: 1500
            })
            return;
        } else if (inputVal) {
            var year = parseInt(inputVal)
            this.queryYear = year
            this.showGanZhi(year, this.chooseMonth, this.chooseDay)
        }
    },
    getJiaZi: function () {
        var m = 0, n = 0
        for (var i = 0; i < 60; i++) {
            jiaZiList[i] = ganList[m] + zhiList[n]
            m++
            n++
            m = m > 9 ? 0 : m
            n = n > 11 ? 0 : n
        }
    },
    getMonthDays(curMonth) {
        if (curMonth == 1 || curMonth == 3 || curMonth == 5 || curMonth == 7 || curMonth == 8 || curMonth == 10 || curMonth == 12) {
            return 31
        }
        if (curMonth == 4 || curMonth == 6 || curMonth == 9 || curMonth == 11) {
            return 30
        }
        if (curMonth == 2) {
            return this.setFebDays()
        }
    },
    //计算二月份天数
    setFebDays() {
        let year = parseInt(this.chooseYear);
        if (year % (year % 100 ? 4 : 400) ? false : true) {
            return 29
        } else {
            return 28
        }
    },
    showGanZhi: function (year, month, day) {
        //重新赋值
        var curYear = parseInt(year + "")
        var curMonth = parseInt(month + "")
        var curDay = parseInt(day + "")

        var longitude = app.globalData.longitude

        var startYear = this.queryYear
        this.chooseYear = curYear
        this.chooseMonth = curMonth
        this.chooseDay = curDay

        var scrNum = 0

        scrNum = curDay < 15 ? 0 : this.sH

        //获取第一年的时间
        var yearDate = new Date((startYear.toString() + "-06-01 00:00:00").replace(/-/g, '/'))
        
        log.info('yingqi-showGanZhi-yearDate-这里是打印当前年份时间', yearDate)

        //获取第一年的干支
        var yearOb = sizhu.getSiZhu(yearDate, longitude)


        log.info('yingqi-showGanZhi-yearOb-这里是打印当前年份时间OB', yearOb)

        var yearGanZhi = yearOb.bz_jn
        var yearNum = jiaZiList.indexOf(yearGanZhi)

        //获取第一月时间
        var monthDate = new Date((curYear.toString() + "-01-15 00:00:00").replace(/-/g, '/'))

        log.info('yingqi-showGanZhi-monthDate-这里是打印当前月份时间', monthDate)

        //获取第一月干支
        var monthOb = sizhu.getSiZhu(monthDate, longitude)

        log.info('yingqi-showGanZhi-monthOb-这里是打印当前月份时间OB', monthOb)

        var monthGanZhi = monthOb.bz_jy
        var monthNum = jiaZiList.indexOf(monthGanZhi)

        //获取第一天时间
        var monthStr = (curMonth.toString() + "").padStart(4, 0)
        var dayDate = new Date((curYear.toString() + "-" + monthStr.toString() + "-01 12:00:00").replace(/-/g, '/'))

        log.info('yingqi-showGanZhi-dayDate-这里是打印当前日期时间', dayDate)

        //获取第一天干支
        var dayOb = sizhu.getSiZhu(dayDate, longitude)

        log.info('yingqi-showGanZhi-dayOb-这里是打印当前日期时间OB', dayOb)

        var dayGanZhi = dayOb.bz_jr
        var dayNum = jiaZiList.indexOf(dayGanZhi)

        /********************************** */
        var yearList = []
        var yearObj = null

        var monthList = []
        var monthObj = null

        var dayList = []
        var dayObj = null

        var dayMonth = ''
        /********************************** */

        //获取年和月
        for (var i = 0; i < 12; i++) {
            yearObj = new Object()
            //循环部分还是取当前年或者搜索年
            yearObj.yearNum = parseInt(startYear + i) + ""
            yearObj.ganZhi = jiaZiList[(yearNum + i) % 60]
            yearObj.check = false
            if (startYear + i == curYear) {
                yearObj.check = true
            }
            yearList.push(yearObj)

            monthObj = new Object
            monthObj.monthNum = parseInt(1 + i) + ""
            monthObj.ganZhi = jiaZiList[(monthNum + i) % 60]
            monthObj.check = false
            if (1 + i == curMonth) {
                monthObj.check = true
                dayMonth = jiaZiList[(monthNum + i + 59) % 60]
            }
            monthList.push(monthObj)
        }

        var monthDays = this.getMonthDays(curMonth)

        var jieQi0 = []
        var jieQi = []
        var jieQi1 = sizhu.getJieQi(curYear)
        var jieQi2 = sizhu.getJieQi(curYear + 1)
        jieQi0 = jieQi1.concat(jieQi2)

        for (var x = 0; x < jieQi0.length; x++) {
            var dateObj = jieQi0[x].date
            var yearObj = dateObj.getFullYear()
            if (yearObj == curYear) {
                jieQi.push(jieQi0[x])
            }
        }
        console.log(jieQi);

        var oneDay = null
        var isRed = true
        for (var i = 0; i < monthDays; i++) {
            dayObj = new Object()
            dayObj.dayNum = parseInt(1 + i) + ""
            dayObj.ganZhi = jiaZiList[(dayNum + i) % 60]
            oneDay = new Date((curYear.toString() + "/" + curMonth.toString() + "/" + parseInt(1 + i).toString()).replace(/-/g, '/'))
            dayObj.lunar = sizhu.getLunarByDate(oneDay)
            dayObj.week = util.getWeekByDate(new Date(oneDay))
            dayObj.check = false
            if (1 + i == curDay) {
                dayObj.check = true
            }
            dayObj.jieQiName = ''
            dayObj.jieQiDate = isRed ? "[" + dayMonth + "月]" : ''
            for (var n = 0; n < jieQi.length; n++) {
                var jieQiName = jieQi[n].jieQi
                var jieQiDate = jieQi[n].date
                var month = jieQiDate.getMonth() + 1
                var day = jieQiDate.getDate()
                if (month == curMonth && day == 1 + i) {
                    dayObj.jieQiName = jieQiName
                    dayObj.jieQiDate = util.formatShortTime(jieQiDate)
                    if (jieQiList.indexOf(jieQiName) % 2 == 0 && isRed == true) {
                        isRed = false
                    } else if (jieQiList.indexOf(jieQiName) % 2 == 1 && isRed == false) {
                        break
                    }
                }
            }
            dayObj.isRed = isRed
            dayList.push(dayObj)
        }

        this.setData({
            yearList: yearList,
            monthList: monthList,
            dayList: dayList,
            scrNum: scrNum,
        })
    },
    yearChoose: function (e) {
        var id = e.currentTarget.dataset.id
        this.showGanZhi(id, this.chooseMonth, this.chooseDay)
    },
    monthChoose: function (e) {
        var id = e.currentTarget.dataset.id
        this.showGanZhi(this.chooseYear, id, this.chooseDay)
    },
    dayChoose: function (e) {
        var id = e.currentTarget.dataset.id
        this.showGanZhi(this.chooseYear, this.chooseMonth, id)
    }
})